package com.heima.sc.user.service;

import com.heima.sc.user.api.OrderApi;
import com.heima.sc.user.mapper.UserMapper;
import com.itheima.sc.pojo.User;
import com.itheima.sc.response.UserOrderDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderApi orderApi;

    public User findOne(String username){
        return userMapper.selectByPrimaryKey(username);
    }

    public List<UserOrderDTO> findUserOrders(){
        //1.获取登录用户名
        String username="liubei";   //集成单点登录
//        2.根据当前用户登录获取 订单数据列表  Feign 对 Hystrix的支持
        List<UserOrderDTO> orderDTOList = orderApi.findOrderDTO(username);
//        3.查询当前用户的信息
        for (UserOrderDTO userOrderDTO : orderDTOList) {
            User user = userMapper.selectByPrimaryKey(userOrderDTO.getUsername());
            BeanUtils.copyProperties(user,userOrderDTO);
        }
//        4.封装返回结果
        return orderDTOList;
    }
}
